package com.gxwy.shenzhou.message;

import com.gxwy.shenzhou.HeaderRequest;
import com.gxwy.shenzhou.SplitString;
import com.gxwy.shenzhou.StringHandler;
import com.gxwy.shenzhou.Supply;


public class ZhangHuMiMaXiuGaiRequest implements StringHandler{
  
  private String zhdh;
  private String ymm;
  private String xmm;
  private int reallen=0;

  private static final int ZHDH_FLAG=2;
  private static final int YMM_FLAG=2;
  private static final int XMM_FAG=2;
  
  private static final int ZHDH_LEN=32;
  private static final int YMM_LEN=6;
  private static final int XMM_LEN=6;
  private static final int TOTAL_LEN=ZHDH_LEN+YMM_LEN+XMM_LEN;

	private HeaderRequest header=new HeaderRequest(true);
public HeaderRequest getHeader() {
	return header;
}
public void setHeader(HeaderRequest header) {
	this.header = header;
}
public String getZhdh() {
	return zhdh;
}
public void setZhdh(String zhdh) throws Exception {
	if(zhdh==null){
		Exception e=new Exception("String is null");
		e.printStackTrace();
	}else if(zhdh.getBytes("gbk").length<ZHDH_LEN){
		this.zhdh=Supply.supply(zhdh,ZHDH_FLAG, ZHDH_LEN);
;
	}else if(zhdh.getBytes("gbk").length>ZHDH_LEN){
		 Exception e=new Exception("Too long");
		 e.printStackTrace();
	}else{
		this.zhdh = zhdh;			
	}
	}
public String getYmm() {
	return ymm;
}
public void setYmm(String ymm) throws Exception {
	if(ymm==null){
		Exception e=new Exception("String is null");
		e.printStackTrace();
	}else if(ymm.getBytes("gbk").length<YMM_LEN){
		this.ymm= Supply.supply(ymm, YMM_FLAG, YMM_LEN);
	}else if(ymm.getBytes("gbk").length>YMM_LEN){
		 Exception e=new Exception("Too long");
		 e.printStackTrace();
	}else{
		this.ymm = ymm;			
	}
}
public String getXmm() {
	return xmm;
}
public void setXmm(String xmm) throws Exception{
	if(xmm==null){
		Exception e=new Exception("String is null");
		e.printStackTrace();
	}else if(xmm.getBytes("gbk").length<XMM_LEN){
		
		this.xmm=Supply.supply(xmm, XMM_FAG, XMM_LEN);;
	}else if(xmm.getBytes("gbk").length>XMM_LEN){
		 Exception e=new Exception("Too long");
		 e.printStackTrace();
	}else{
		this.xmm = xmm;			
	}
}
public String build () throws Exception{
	if(this.getHeader()==null){
		throw new Exception("Header is null");
	}
	if(this.getZhdh()==null){
		this.setZhdh("");
	}
	if(this.getYmm()==null){
		this.setYmm("");
	}
	if(this.getXmm()==null){
		this.setZhdh("");
	}
	Integer totallen=header.TOTAL_LEN+TOTAL_LEN-HeaderRequest.LEN_LEN;
     this.getHeader().setLen(totallen.toString());
    	return  this.getHeader().build()+this.getZhdh()+this.getYmm()+this.getXmm();
	}
public void parse(String str) throws Exception{
	if(str==null){
		Exception e=new Exception("String is null");
		e.printStackTrace();
	}else if(str.getBytes("gbk").length<TOTAL_LEN+header.TOTAL_LEN){
		Exception e=new Exception("Invalid request");
		e.printStackTrace();
	}else{  
		String headerLen=SplitString.splitString(str,reallen, reallen+header.TOTAL_LEN);
		reallen+=header.TOTAL_LEN;
		
		header.parse(headerLen);
		this.setZhdh(SplitString.splitString(str,reallen,reallen+ZHDH_LEN));
		reallen+=ZHDH_LEN;
        this.setYmm(SplitString.splitString(str,reallen,reallen+YMM_LEN));
       reallen+=YMM_LEN;
		this.setXmm(SplitString.splitString(str,reallen,reallen+XMM_LEN));
	 
	
	}
	

  }
public static void main(String args[]) throws Exception{
	 ZhangHuMiMaXiuGaiRequest	abc=new ZhangHuMiMaXiuGaiRequest();
	 abc.setZhdh("9");
	 abc.setYmm("3");
	 abc.setXmm("2");
	 String a=abc.build();
	 abc.parse(a);
     if(abc.getZhdh().trim().equals("9")){
    	 System.out.println("zhdhyes");
     }
     if(abc.getYmm().trim().equals("3")){
    	 System.out.println("Ymmyes");
     }
     if(abc.getXmm().trim().equals("2")){
    	 System.out.println("Xmmyes");
     }

}
}